const _ = require('lodash')

const old_obj = {
    a: 1,
    b: 'bb',
    c: new Date(),
    d: function(){},
    e: [1,2,3]
}

console.log(_.cloneDeep(old_obj))

const new_obj = JSON.parse(JSON.stringify(old_obj))
console.log(new_obj)

// 使用Object.prototype.toString().call实现类型判断；其他还有typeof/instanceof
function deepClone(object){
   const type = Object.prototype.toString.call(object)
   console.log(type)
    if(type === '[object Object]'){
        const new_obj = new Object()
        for (const key in object) {
            if (Object.hasOwnProperty.call(object, key)) {
                new_obj[key] = deepClone(object[key])
            }
        }
        return new_obj
    }else if(type === '[object Array]'){
        const new_arr = new Array()
        object.forEach((element) => {
            new_arr.push(deepClone(element))
        });
        return new_arr
    }else{
        return object
    }
}

console.log(deepClone(old_obj))




